package com.taobao.machinesystem.service.impl;

import com.taobao.machinesystem.entity.SysRole;
import com.taobao.machinesystem.entity.SysRolePermission;
import com.taobao.machinesystem.mapper.SysRoleMapper;
import com.taobao.machinesystem.service.SysRolePermissionService;
import com.taobao.machinesystem.service.SysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.management.relation.Role;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiuJunCheng
 * @since 2022-04-22
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createRolePermission(String roleId, String[] permissionIds) {
        SysRolePermission sysRolePermission = new SysRolePermission();
        sysRolePermission.setRoleId(roleId);
        for (String permissionId : permissionIds) {
            sysRolePermission.setRoleId(permissionId);
            sysRolePermissionService.save(sysRolePermission);
        }
        return true;
    }

    @Override
    public int createRole(SysRole role) {
        int insert = baseMapper.insert(role);
        return insert;
    }
}
